//
//  WBStatusPictureView.swift
//  微博
//
//  Created by 康智桐 on 2018/5/4.
//  Copyright © 2018年 Kk. All rights reserved.
//

import UIKit

class WBStatusPictureView: UIView {

    @IBOutlet weak var heightCons : NSLayoutConstraint!

    
    
    var viewMode:WBStatusViewModel? {
        
        didSet{
            calcViewSize()
            //设置urls
            urls = viewMode?.picURLs
        }
        
    }
    
    //根据视图模型的配图大小，调整显示内容
    func calcViewSize(){
        
        //处理宽度
        //单图 根据配图视图的大小，修改subview【0】的宽高
        if viewMode?.picURLs?.count == 1 {
            
            let viewSize = viewMode?.pictrueViewSize ?? CGSize()
            
            let v = subviews[0]
            
            v.frame = CGRect(x: 0,
                             y: WBStatusPictureViewOutterMargin,
                             width: viewSize.width,
                             height: viewSize.height - WBStatusPictureViewOutterMargin)
            
            
        }else {
            //多图，恢复subview【0】的宽高，保证九宫格的完整性
            let v = subviews[0]
            v.frame = CGRect(x: 0,
                             y: WBStatusPictureViewOutterMargin,
                             width: WBStatusPictureItemWidth,
                             height: WBStatusPictureItemWidth)
            
            
            
            
        }
        
        //修改高度约束
        heightCons.constant = viewMode?.pictrueViewSize.height ?? 0

    }
    
    
    
    
    
    
    
    
    //配图视图的数组
    
   private var urls:[WBStatusPicture]?{
        didSet{
            
            //影藏所有的imagev
            for v in subviews {
                v.isHidden = true
            }
            
            var index = 0
            //遍历urls数组，顺序设置图像
            
            for url in urls ?? [] {
            
                //获得对应索引的imageview
                let iv = subviews[index] as! UIImageView
                
                //1张图像处理
                if index == 1 && urls?.count == 4 {
                    index += 1
                }
                
                
                
                //设置图像
                iv.cz_setImage(urlString: url.thumbnail_pic, placeholderImage: nil)
                
                //显示图像
                iv.isHidden = false
                
                index += 1
            
            }
        }
    }
    
    
    
    
    override func awakeFromNib() {
        setupUI()
    }
    
    
}

extension WBStatusPictureView {
    ///cell 中所有控件都是提前准备好
    
    
    
    
    func setupUI(){
        
        
        //设置背景颜色
        backgroundColor = superview?.backgroundColor
        
        
        clipsToBounds = true
        
        let count = 3
        let rect = CGRect(x: 0, y: WBStatusPictureViewOutterMargin, width: WBStatusPictureItemWidth, height: WBStatusPictureItemWidth)
        
        //循环创建9个imageview
        for i in 0..<count * count {
            
            let iv = UIImageView()
            
            
            //设置 contentmode
            iv.contentMode = .scaleAspectFill
            iv.clipsToBounds = true
        
            
            //行
            let row = CGFloat( i / count)
            
            //列
            let col = CGFloat( i % count)
            
            
            let xOffset = col * (WBStatusPictureItemWidth + WBStatusPictureViewInnerMargin)
            let yOffset = row * (WBStatusPictureItemWidth + WBStatusPictureViewInnerMargin)
            
            iv.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
            
        
            addSubview(iv)
            
        }
        
        
        
    }
    
}
